package com.opener.common.code.assistant.gen;

import com.opener.common.code.assistant.model.Prop;
import com.opener.common.code.assistant.model.Tab;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

public class ModelGenerator extends Generator{
    private static String Suffix = "";

    public ModelGenerator(Ctx ctx, Ctx.MvcClass mvcClass) {
        super(ctx, mvcClass);
    }


    @Override
    public String createJavaString(){
        Ctx.MvcClass cls = getMvcClass();
        Tab tab = cls.getTab();

        String pkgLine = "package "+cls.getPkg()+";\n\n";
        SortedSet importLines = new TreeSet();
        importLines.add("import com.opener.common.model.Model;");
        StringBuffer classBody = new StringBuffer();
        classBody.append("public class "+cls.getClassName()+" extends Model{\n");
        List<Prop> props = tab.getProps();
        for (int i = 0; i< props.size(); i++){
            Prop prop = props.get(i);

            if ("Date".equalsIgnoreCase(prop.getJavaType())){
                importLines.add("import java.util."+prop.getJavaType()+";");
            }else if ("List".equalsIgnoreCase(prop.getJavaType())){
                importLines.add("import java.util."+prop.getJavaType()+";");
            }else if ("BigDecimal".equalsIgnoreCase(prop.getJavaType())){
                importLines.add("import java.math."+prop.getJavaType()+";");
            }else if (!"boolean".equalsIgnoreCase(prop.getJavaType())){
                importLines.add("import java.lang."+prop.getJavaType()+";");
            }
            if (prop.getComment() != null && prop.getComment().length()>0){
                String comment =  prop.getComment();
                classBody.append("\t/**\n");
                classBody.append("\t* "+comment+"\n");
                classBody.append("\t*/\n");
            }
            classBody.append("\tprivate "+prop.getJavaType()+" "+prop.getJavaProp()+";\n");
        }
        classBody.append("}");
        String clsStr = pkgLine +
                StringUtils.collectionToDelimitedString(importLines,"\n") +"\n\n"+
                createClassDescString()+
                classBody.toString();
        return clsStr;
    }


}
